goog.provide('czk.ui.grid.Paging');

goog.require('goog.dom');
goog.require('goog.dom.TagName');

goog.require('czk.ui.ZKComponent');

/**
 * @constructor
 * @extends {czk.ui.ZKComponent}
 */
czk.ui.grid.Paging = function() {
  goog.base(this);
  this.hasZKChildren = false;
  this.activePage = 0;
};
goog.inherits(czk.ui.grid.Paging, czk.ui.ZKComponent);

czk.ui.grid.Paging.prototype.select = null;

czk.ui.grid.Paging.prototype.llA = null;
czk.ui.grid.Paging.prototype.lA = null;
czk.ui.grid.Paging.prototype.rA = null;
czk.ui.grid.Paging.prototype.rrA = null;

czk.ui.grid.Paging.prototype.activePage = 1;
czk.ui.grid.Paging.prototype.totalSize = null;
czk.ui.grid.Paging.prototype.pageSize = null;
czk.ui.grid.Paging.prototype.pageCount = null;

czk.ui.grid.Paging.prototype.initialize =
    function(name, parameter, portlet, children) {
      goog.base(this, 'initialize', name, parameter, portlet, children);
      if (parameter) {
        if (parameter['activePage'])
          this.activePage = parameter['activePage'];
        else
          this.activePage = 0;
        if (parameter['totalSize'])
          this.totalSize = parameter['totalSize'];
        if (parameter['pageSize'])
          this.pageSize = parameter['pageSize'];
        if (parameter['pageCount']) {
          this.pageCount = parameter['pageCount'];
        }
      }
    };

czk.ui.grid.Paging.prototype.createDom = function() {
  var dh = this.getDomHelper();
  var el = dh.createDom(goog.dom.TagName.DIV, {
    'className': 'table_navigation_area'
  });
  this.setElementInternal(el);
  this.createBlockChanger(el);
  this.createPages(el);
};

czk.ui.grid.Paging.prototype.createBlockChanger =
    function(el) {
      var dh = this.getDomHelper();
      var el1 = dh.createDom(goog.dom.TagName.DIV, {
        'className': 'tableBlockChanger'
      });
      goog.dom.insertChildAt(el, el1);
      var el2 = dh.createDom(goog.dom.TagName.DIV, {
        'className': 'tableBlockChangerList'
      });
      goog.dom.insertChildAt(el1, el2);

      this.select =
          new goog.ui.Select('Select action ...', null,
              goog.ui.FlatMenuButtonRenderer.getInstance());
      this.addChild(this.select);
      this.select.render(el2);
      var pa = [ '5', '10', '20', '50', '100' ];
      goog.object.forEach(pa, function(p) {
        this.select.addItem(new goog.ui.MenuItem(p + ' per page', p));
      }, this);
    };

czk.ui.grid.Paging.prototype.createPages = function(el) {
  var dh = this.getDomHelper();
  var el1 = dh.createDom(goog.dom.TagName.DIV, {
    'className': 'pages'
  });
  goog.dom.insertChildAt(el, el1);

  this.llA = dh.createDom(goog.dom.TagName.A, {
    'className': 'left2'
  });
  this.lA = dh.createDom(goog.dom.TagName.A, {
    'className': 'left0'
  });
  this.rA = dh.createDom(goog.dom.TagName.A, {
    'className': 'right0'
  });
  this.rrA = dh.createDom(goog.dom.TagName.A, {
    'className': 'right2'
  });
  goog.dom.insertChildAt(el1, dh.createDom(goog.dom.TagName.SPAN, {
    'className': 'left2out'
  }, this.llA));
  goog.dom.insertChildAt(el1, dh.createDom(goog.dom.TagName.SPAN, {
    'className': 'left0out'
  }, this.lA));
  goog.dom.insertChildAt(el1, dh.createDom(goog.dom.TagName.SPAN, {
    'className': 'right0out'
  }, this.rA));
  goog.dom.insertChildAt(el1, dh.createDom(goog.dom.TagName.SPAN, {
    'className': 'right2out'
  }, this.rrA));

  goog.dom.insertChildAt(el, dh.createDom(goog.dom.TagName.DIV, {
    'className': 'status'
  }, 'Page ' + (this.activePage + 1) + ' of ' + this.pageCount));

};

czk.ui.grid.Paging.prototype.enterDocument =
    function() {
      goog.base(this, 'enterDocument');
      this.getHandler().listen(this.rA, goog.events.EventType.CLICK,
          this.rAction, false, this);
      this.getHandler().listen(this.rrA, goog.events.EventType.CLICK,
          this.rrAction, false, this);
      this.getHandler().listen(this.lA, goog.events.EventType.CLICK,
          this.lAction, false, this);
      this.getHandler().listen(this.llA, goog.events.EventType.CLICK,
          this.llAction, false, this);
      if (this.select != undefined)
        this.getHandler().listen(this.select,
            goog.ui.Component.EventType.ACTION, this.handleSelectAction, false,
            this);
    };

czk.ui.grid.Paging.prototype.addZKChildToElement = function() {
  return true;
};

czk.ui.grid.Paging.prototype.handleSelectAction = function(e) {
  var select = e.target;
  var val = select.getValue();
  this.portlet.sendXhr(this.getParent(), 'onPageSize', '{"size":' + val + '}');
};

czk.ui.grid.Paging.prototype.llAction = function(e) {
  this.portlet.sendXhr(this, 'onPaging', '{"":0}');
};
czk.ui.grid.Paging.prototype.lAction = function(e) {
  var toPage = this.activePage - 1;
  if (toPage < 0)
    toPage = 1;
  this.portlet.sendXhr(this, 'onPaging', '{"":' + toPage + '}');
};
czk.ui.grid.Paging.prototype.rAction = function(e) {
  var toPage = this.activePage + 1;
  if (toPage > this.pageCount)
    toPage = this.pageCount;
  this.portlet.sendXhr(this, 'onPaging', '{"":' + toPage + '}');
};
czk.ui.grid.Paging.prototype.rrAction = function(e) {
  this.portlet.sendXhr(this, 'onPaging', '{"":' + this.pageCount + '}');
};

czk.ui.grid.Paging.create = function() {
  return new czk.ui.grid.Paging();
};
